
const app = getApp();
var http = require('../../service/http.js');//请求类
var util = require('../../utils/util.js');//工具类
// 引入SDK核心类
var QQMapWX = require('../../utils/qqMap/qqmap-wx-jssdk.min.js');

// 实例化API核心类
var demo = new QQMapWX({
  key: '2UBBZ-N45WV-RHTPK-UOUWB-SVK7E-XMBCA' // 必填
});
var adTime = '';//用于清除计时事件
Page({
  data: {
    //---当前页名称--------------
    pageName:'home',
    isShowAD: 'notYet',//是否展示首页的投放广告
    adDetailArr: [],//广告信息
    adInterval: '',//每张广告投放时间间隔
    countAdTime: '',//广告计时时间
    closeAdAnimationData:'',//广告隐藏动画
    showPage:false,//是否展示页面
    cityName:'',//程序内默认city
    cityArray : [],//全部城市
    
    AreaIcon:true,//地区选择图标（true--未展开）
    //-----登录--------------------------
    loginCode: '',//用于登录获取的code,5分钟之内有效
    codeExpiryTime:300000,//code过期时间
    isAuthorization: 'notYet',//是否授权登陆
    userInfoScope: false,//用户授权状态
    animationData: '',//用户登陆动画
    isLogin :false,//是否点击了登录按钮

    showLogin: 'notYet',//是否展示登录
    isshow: false,//是否显示mask
    toBuy: false,//是否是立即购买

    //首页广告跳转
    adProId: '',//广告携带商品id
    carouseImgArr: [],
    defalutImgArr: [
      'https://mamatx.com.cn/order/carouselImgs/1529372998590.jpg',
      'https://mamatx.com.cn/order/carouselImgs/1529373009387.jpg',
      'https://mamatx.com.cn/order/carouselImgs/1529373018996.jpg'
    ],//默认广告图片
    indicatorDots: true,//true
    autoplay: true,//开启自动切换
    interval: 4000,//自动切换时间间隔
    duration: 500,//滑动时长
    showLoadding:true,//加载动画gif
    showLine1:false,//首页更多精彩横排展示几个商品false --两个 true 1个
    imgHeight:'',//商品图片高度
    currentSwiper:0,//当前播放图片序号

    //---商品相关---------------------------------------------------
    products:[],//所有商品
    hotPorducts: [],//热销商品
    productId:'',//当前商品id
    pageNo:1,//商品列表分页
    total:0,//商品分页总数
    pageSize: '',//分页查询商品数量
    product:{},//商品
    minPrice: 0,//最小价格（初始化商品价格范围）
    maxPrice: 0,//最大价格
    skuTitles: [],//sku中title
    isActive: false,//是否被选中
    skus: [],//所有sku内容
    distAttrs: [],//商品sku属性及属性值信息
    chooseAttrids: [],//已选属性id
    //----------暂存购物车商品信息-------------------
    itemId: '',//暂存购物车商品id 
    skuId: '',//skuid
    count: 1,//购物车数量
    stock: 0,  //默认库存
    //addCart:false,//初始化购物车动画
    //----------分享 绘图----------------------------
    showCanvas:false,//是否展示绘图
 
    //-----------跳转地址---------------------------
    pageUrl:'',//跳转地址
    //----------商品分享分享人inviter-店铺id------------------------------
    inviter: '',//分享人inviter(通过二维码扫描进入小程序获取)
    shopuid: '',//店铺id
    shopName: '',//店铺名称
    isOwnShop:'',//是否是自己有分销店铺
    inShareShop: false,//是否在分享店铺
    shareKey: '',//shareKey =='0'为分享标识

    shareShopuid:'',//分享店铺id

    //sku所有价格---
    skuPrices:[],

    //测试
    noticeScrollTop: 1, // 通知滚动条滚动高度
    isBottom:false,//是否触底
  },
  onLoad: function (option) {
    wx.hideTabBar();
    

    //加载首页及计时广告成功回调（控制广告及页面展示顺序）
    var loadAdTask = setInterval(function(){

      let isShowAD = app.data.isShowAD;

      if (isShowAD === false || isShowAD === true){
        clearInterval(loadAdTask);//结束周期任务
        if (isShowAD) {

          console.log('有广告app.data.isShowAD:' + app.data.isShowAD);
          that.setData({
            adDetailArr: app.data.adDetailArr,//播放广告内容
            adInterval: app.data.adInterval,//时长间隔
            countAdTime: app.data.countAdTime,//计时时间
            isShowAD: app.data.isShowAD,//是否展示广告
          })
          //广告计时
          that.countAdTime();

        } else {//没有广告且已经登录

          console.log('无广告app.data.isShowAD:' + app.data.isShowAD);
          //初始化广告数据 
          that.setData({
            adDetailArr: [],//播放广告内容
            adInterval: '',//时长间隔
            countAdTime: '',//计时时间
            isShowAD: false,//是否展示广告
          })

        }
        // 展示页面、检查是否需要登录
        setTimeout(function () {
          that.setData({
            showPage: true        
          })
        },100)
      }
      
    },100);
    
    console.log("进入homejs-onLoad：本地店铺信息")
    console.log(wx.getStorageSync('shopInfo'));
    console.log("homejs页onload分享参数");
    console.log(option);

    let that = this;

    let shopInfo = wx.getStorageSync('shopInfo') || {};//店铺信息、是否有绑定或自己的店铺

    let isAuthorization = app.data.isAuthorization;
    //--------------------------分享店铺参数------------------------------------
    //本地店铺信息、店铺id、店铺名称、分享标识、
    let [shopuid, isOwnShop, shareKey,inviter] = [shopInfo.shopuid || '', shopInfo.isOwnShop ||'', '',''];

    //分享参数信息
    if (option.parms) {
      //店铺id 、店铺名称 、分享标识
      let paramArray = option.parms.split('/');
      console.log('paramArray=' + paramArray);
      shopuid = paramArray[0];
      inviter = paramArray[1];
      shareKey = paramArray[2];
    } else if (option.scene){
      console.log(option.scene);
      let scene = decodeURIComponent(option.scene);
      let param = scene.split('/');
      shopuid = param[0];//商品id
      inviter = param[1];//分享人uid
      shareKey = param[2];//转发标识
    }
    //设置分享店铺信息
    that.setData({
      scrollViewHeight: app.data.scrollViewHeight,//滚动窗口高度
      shopuid: shopuid,
      shareKey: shareKey,
      inviter: inviter,
    })
 
    console.log("shopuid:" + shopuid + "inviter：" + inviter + "shareKey:" + shareKey);
  
    //如果用户已经登录 查看本地店铺信息----已经登录并且有自己的店铺，直接查找商品详情
    that.waitChecksessionTask(); //检查登录态

    //显示底部菜单任务（显示菜单条件：1.广告关闭 2.登录窗关闭 3.登录授权成功）
    let loginTemp = setInterval(function () {

      let isAuthorization = that.data.isAuthorization;//是否已经授权登录
      let showLogin = that.data.showLogin;//是否展示登录窗
      let isShowAD = that.data.isShowAD;//是否展示广告

      if (isAuthorization === true && showLogin === false && isShowAD === false ) {
        clearInterval(loginTemp);
        //展示底部菜单
        wx.showTabBar();
        //店铺定位
        that.getLocation();//获取page变量cityName
      }

    }, 100);

  },
  
  onShow:function(){
    let that = this;

    app.checkSession(that);
    //刷新banner轮播
    this.getCarouseImgArr();
    
    //改变商品图片父级高度
    this.changeImgHeight();
    //检查登录是否过期
    setTimeout(function(){
      console.log('检查登录过期结束');
      let loginMsg = app.data.loginMsg;
      if (loginMsg == 'SessionTimeOut') {
        wx.hideTabBar();//隐藏底部菜单
        that.loginAnimation();
      }
    },3000)

  },
  waitChecksessionTask: function () {//检查登录态
    let that = this;
	let shareKey = that.data.shareKey;
    var waitChecksessionTask = setInterval(function () {

      let isShowAD = that.data.isShowAD;

      if (isShowAD === true || isShowAD === false) {
        let isAuthorization = app.data.isAuthorization;
        let shopInfo = wx.getStorageSync('shopInfo') || {};

        if (isAuthorization === true) {//用户已经登录
          clearInterval(waitChecksessionTask);
          if (shopInfo.isOwnShop !== '1' && shareKey == '0') {//用户已登录 没店铺
            that.setShopInfo();
          } else {//不是自己的店铺
            that.onPullDownRefresh(); //加载商品列表
            app.setShopTitle();//设置店铺title
          }
          //设置授权信息
          that.setData({
            isAuthorization: isAuthorization,
            showLogin: false,
            isBindPhone: wx.getStorageSync('isBindPhone'), //根据绑定状态获取用户授权
          });

        } else {//未登录
          let showLogin = that.data.showLogin
          if (showLogin !== true) {//未展示登录
            that.loginAnimation();
            return;
          }
        }
      }
    }, 150)
  },
  onReady:function(res){
 
  },
 
  //-------登陆动画-------------------
  loginAnimation: function () {
    
    app.loginAnimation();//this 用于传递 userlogin 的分享店铺信息
    this.setData({
      isAuthorization: app.data.isAuthorization,
      animationData: app.data.animationData,
      showLogin: app.data.showLogin,
    })
    
  },

  onShareAppMessage: function (options) {
    console.log("分享来源：")
    console.log(options);
    let shopInfo = wx.getStorageSync('shopInfo') || '';
    let shopName = '';//转发携带店铺名称
    let shopuid  = '';//店铺id
    if (shopInfo){
      shopName = shopInfo.shopName || '';
      shopuid = shopInfo.shopuid || '';
    }
    let inviter = wx.getStorageSync('unionid');//分享人信息
    console.log('分享店铺信息：' + shopInfo);
    return {
      title: '咱+ ' + shopName,
      path: '/pages/home/home?parms=' + shopuid  + '/' + inviter +'/0'   //店铺id 、店铺名称 、分享标识
    }
  },
  //下拉刷新 加载全部商品
  onPullDownRefresh:function(){
    console.log("下拉刷新,根据shopuid 获取商品列表");
    let that = this;
    //隐藏加载动画
    that.setData({
      showLoadding: true,
    })
   
    //根据city查询商品列表
    let loadListByCityNameTask = setInterval(function(){
      let cityName = app.data.cityName;
      if (cityName != '' ){

        console.log('根据shopuid和cityName加载商品列表');

        clearInterval(loadListByCityNameTask);

        // 获取商品列表-------------------------------------------------------
        let shopInfo = wx.getStorageSync("shopInfo");
        let [pageNo, shopuid] = [ that.data.pageNo, ''];
        shopuid = shopInfo.shopuid || shopuid;

        that.loadHotItemList(shopuid, cityName);//加载热门商品列表
        that.loadItemList(pageNo, shopuid, cityName); //加载普通商品列表（分页）
        that.setData({
          cityArray: app.data.cityArray,//城市集合
        })
      }

    },100);
  },

  getLocation:function(){
    console.log('获取地理位置信息');
    let that = this;
    let cityName = app.data.cityName;
    
    if (cityName == '成都'){

      that.setCityName(cityName);//设置地理位置
      
    }else{//获取本地地理位置
      cityName = wx.getStorageSync('cityName') || '';
      console.log('本地cityName=' + cityName);
      //初始化城市信息、本地位置信息为空，获取地理位置
      if (!cityName) {
        console.log('解析地理位置');
        wx.getLocation({
          type: 'gcj02',
          success: function (res) {
            console.log(res);
            let [longitude, latitude] = [res.longitude, res.latitude];//用户所在经纬度
            console.log('解析城市');
            that.loadCity(longitude, latitude);//解析地理位置--加载城市
            // that.loadCity(117.12, 39.08);//解析地理位置--加载城市
          },
          fail: function (res) {// 拒绝获取地理位置
            that.setCityName();//设置地理位置
          },
          complete: function (res) { },
        })
      }else{//本地有城市设置城市
        that.setCityName(cityName);//设置地理位置
      }
    }

  },

  //根据商品id 查商品详情
  selectProductById:function(e){
    console.log('根据商品id 查商品详情参数');
    console.log(e);
    let productId = e.currentTarget.dataset.productid || this.data.productId;
    let isAdImg = e.currentTarget.dataset.type;//商品跳转类型
    let setTime = isAdImg ? 1000 : 0;
    let that = this;
    console.log(productId);
    this.setData({
      productId: productId,
      pageUrl: '/pages/product/product'
    })
    //广告进入商品操作
    let isShowAD = this.data.isShowAD;
    if (isShowAD) {//关闭广告和计时
      this.closeAdAnimation();
    }
    let isAuthorization = app.data.isAuthorization;
    if (isAuthorization === true){
      setTimeout(function(){
        app.selectProductById(that);
      },setTime)
    }else{
      util.showToast('请小主先登录咱加~', 1500, 'none', function () {
        wx.hideTabBar();
        that.loginAnimation();
       });
       return
    }
    
  },
  //跳转到搜索页
  toSearchPage:function(e){
    
    this.getFormId(e);
    console.log('跳转到搜索页');
    console.log(e);
    wx.navigateTo({
      url: '/pages/home/pages/search/search'
    })
  },
  //跳转更多分类
  toCategoryPage:function(e){
    console.log(e);
    app.toCategoryPage(e);
    console.log("toCategoryPage跳转更多分类");
  },
  //加载更多
  onReachBottom:function(e){
    console.log('加载更多');
    console.log(e);
    let [pageNo, total, that] = [this.data.pageNo, this.data.total, this];
    if (pageNo < total){
      //加载动画
      this.setData({
        showLoadding: true
      })
      
      pageNo = pageNo + 1;
      let products = that.data.products;//商品列表
      let [shopInfo, shopuid] = [wx.getStorageSync('shopInfo'), ''];

      if (shopInfo.shopuid) {
        shopuid = shopInfo.shopuid;
      }
      that.loadItemList(pageNo, shopuid);
    }
  },
  //点击用户登录按钮
  getUserInfo:function(e){
    console.log('用户登录');

    //查看是否允许授权
    let [that, errMsg] = [this, e.detail.errMsg];

    if (errMsg == 'getUserInfo:ok'){

      that.userLogin(e);

    }else{//未授权，打开设置

      //设置登录按钮状态
      that.setData({
        isLogin :false,//此时用户可点击登录按钮
      })
      
      app.connectionTimeout('请允许获得您的公开信息（头像、昵称）用于登录');
     
    }
  },
  // 用户登录
  userLogin:function(e){

    //查看是否允许授权
    let [that,  shareKey] = [this, this.data.shareKey];

    let shopInfo = wx.getStorageSync('shopInfo') || {};
    let isAuthorization = '';
    let pageUrl = that.data.pageUrl;

    that.setData({
      isLogin: true,//是否点击登录按钮
    })

    app.userLogin(e,that);//携带onload分享的shopuid 数据

    //周期检查登录态 （登录请求回调后：1.关闭动画2.设置并保存店铺信息到本地3.）
    var loginInterval = setInterval(function () {

      isAuthorization = app.data.isAuthorization;
      if (isAuthorization === true) {//已登录成功
        wx.showTabBar();
        clearInterval(loginInterval);//停止周期循环任务
        that.cancelAnimation();//登录后关闭登录动画

        shopInfo = wx.getStorageSync('shopInfo');
        if (shareKey == '0' && shopInfo.isOwnShop !== '1') {//没有自己的店铺
          //设置店铺信息 根据店铺信息查找订单、购物车数量
          that.setShopInfo();
        } else {
          //设置店铺标题
          app.setShopTitle();
          //刷新商品列表
          that.onPullDownRefresh();
        }

        that.setData({
          isAuthorization: app.data.isAuthorization,
          showLogin: false,
          isBindPhone: wx.getStorageSync('isBindPhone'), //根据绑定状态获取用户授权
        })

        //-------------（点击首页广告）跳转至商品详情---------------
        if (pageUrl != '') {
          setTimeout(function(){
            app.selectProductById(that);
          },1000)
        }
      }
    }, 100)
    //十秒钟提示网络链接超时重试
    setTimeout(function(){
      //网络链接超时的可能
      if (!isAuthorization){
        clearInterval(loginInterval);//停止周期循环任务
        console.log('客户端登录情况isAuthorization：' + isAuthorization);
        //超时重新登录提示
        app.connectionTimeout("网络链接异常,请重新登录");
        that.setData({
          isLogin :false,//是否点击了登录按钮
        })
      }else{
        console.log('定时任务结束');
      }
    },1000*10);
  },

  
  //设置店铺信息
  setShopInfo: function () {
    console.log('setShopInfo');

    let [that, shopuid, inviter, isOwnShop, shopInfo] = [this, this.data.shopuid, this.data.inviter || '', '', wx.getStorageSync('shopInfo') || {}];
 
    //如果是用户绑定的不是自己的店铺，而且没有分享店铺id,查询分享人有没有店铺
    http.getReq('/weixin/getShopInviter', {
      shopuid: shopuid,
      inviter: inviter
    }, function (res) {
      console.log('setShopInfo 返回信息');
      console.log(res);
      //店铺信息保存在本地---------------------------------------------
      if (res.data.shopuid && shopInfo.isOwnShop !== '1') {
        //设置参数
        //更新本地店铺数据
        shopInfo.shopuid = res.data.shopuid; //店铺id
        shopInfo.shopName = res.data.shopName;//店铺名称
        shopInfo.isOwnShop = '';//是否为自己的店铺
      }

      //更新店铺信息,若是平台分享则保留原有 shopInfo
      wx.setStorageSync('shopInfo', shopInfo);
      console.log('显示的店铺信息应为：');
      console.log(wx.getStorageSync('shopInfo'));

      //刷新商品列表、设置店铺title
      that.onPullDownRefresh();
      app.setShopTitle();
    });
   
  }
  ,  //------------------关闭/开启购物车动画----------------
  cancelAnimation: function (toCancel) {
    let that = this;
    app.cancelAnimation();
    //showSkuList、shareProduct分别为判断显示弹出内容
    this.setData({
      showSkuList: false,
      shareProduct: false,
      animationData: app.data.animationData,//加入购物车动画
    })
    //动画延时播放
    setTimeout(function () {
      that.setData({
        isshow: app.data.isshow,
        showLogin: app.data.showLogin,
        animationData: app.data.animationData,//加入购物车动画
        isLogin: false,//是否点击了登录按钮
      })
    }.bind(this), 300)
  },
  //商品列表缩略图 图片加载失败出现错误时替换图片路径
  errImg: function (e) {
    let proIndex = e.currentTarget.dataset.proindex;//商品下标
    let [products, hotPorducts] = [[], []];
    let isSellHot = e.currentTarget.dataset.issellhot;//是否是热销列表

    if (isSellHot == '1') {//热销列表
      hotPorducts = this.data.hotPorducts;
      this.setData({
        hotPorducts: app.errImg(hotPorducts, proIndex)//返回products 更改thumbImgs路径
      })
    } else {//普通商品列表
      products = this.data.products;
      this.setData({
        products: app.errImg(products, proIndex)//返回products 更改thumbImgs路径
      })
    }
  },
  //连接客服中
  connectService:function(e){
    console.log(e);
    console.log("连接客服中");
  },
  //------------------未开通服务提示----------------------
  unOpen: function () {
    app.unOpen();
  },
  //手动跳过广告
  closeAd:function(){
    clearTimeout(adTime);
    this.closeAdAnimation();//关闭动画
  },
//倒计时显示广告
  countAdTime:function(){
    let isShowAD = this.data.isShowAD;

    if (isShowAD) {//如果展示广告
      // wx.hideTabBar();
      console.log("倒计时---" + this.data.countAdTime);
      let [that, countAdTime] = [this, this.data.countAdTime]

      countAdTime = countAdTime - 1;
      if (countAdTime == 0) {
        console.log("停止倒计时 getCode:false");
        clearTimeout(adTime);
        this.closeAdAnimation();//关闭动画
        return;
      } else {
        that.setData({
          countAdTime: countAdTime
        })
        adTime = setTimeout(function () {
          that.countAdTime();
        }, 1000)
      }
    }
  },
  //关闭广告动画
  closeAdAnimation:function(){
    let that = this;
    clearTimeout(adTime);
    let animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease',
    })
    this.animation = animation
    animation.scale(0, 0).opacity(0.2).step()

    this.setData({
      closeAdAnimationData: animation.export(),
    })

    setTimeout(function () {
      that.setData({
        countAdTime: app.data.countAdTime,//初始化验证码过期时间
        isShowAD: false,
      })
    },400)
  },

  //获取banner轮播
  getCarouseImgArr: function () {
    console.log('获取banner轮播');
    let that = this;

    http.getReq('/weixin/zjCarouseImg/list', {
      cityName: wx.getStorageSync('cityName') || '北京'
    }, function (res) {
      console.log(res);
      let interval = res.interval + "000";  //自动切换时间间隔；
      let carouseImgArr = [];
      if (res.carouseImgArr.length > 0) {
        carouseImgArr = res.carouseImgArr;//图片src、商品itemId
      } else {
        carouseImgArr = [];
      }

      that.setData({
        carouseImgArr: carouseImgArr,
        interval: interval || '',
        currentSwiper: 0,//当前播放图片序号
      })
    });
    
  },
  getFormId:function(e){
    console.log(e);
    app.getFormId(e);
  },
  //变更展示一行展示多少商品数
  changeLineNum:function(){
    this.setData({
      showLine1: !this.data.showLine1
    })
  },
  changeImgHeight:function(){
    let that = this;
    wx.createSelectorQuery().selectAll('.proSection1').boundingClientRect(function (rects) {
      let imgHeight = ''
      rects.forEach(function (rect) {
        imgHeight = rect.width ;  // 节点的宽度
      })
      that.setData({
        imgHeight: imgHeight
      })
    }).exec()
  },
  //加载热门商品列表 --参数shopuid
  loadHotItemList: function (shopuid,cityName){
    
    console.log("请求首页商品列表shopuid:" + shopuid);
    let that = this;

    http.getReq('/weixin/item/hotItemList', {
      shopuid: shopuid,//用于根据店铺id查找该店铺上架的商品
      cityName: cityName
    }, function (res) {
      let hotPorducts = res.data;

      for (let p = 0; p < hotPorducts.length; p++) {

        let skus = hotPorducts[p].skus;
        if (skus.length > 0) {
          let skuPrices = [];

          for (let s = 0; s < skus.length; s++) {
            skuPrices.push(parseFloat(skus[s].price));
          }

          let minPrice = Math.min(...skuPrices);
          let maxPrice = Math.max(...skuPrices);
          hotPorducts[p].minPrice = minPrice;
          hotPorducts[p].maxPrice = maxPrice;

        }
      }

      console.log('hotPorducts列表:');
      console.log(hotPorducts);

      //设置热销商品数据
      that.setData({
        hotPorducts: hotPorducts,
        showLoadding: false    //隐藏加载动画
      })
    });

  },
  //分页加载全部商品列表--参数 pageNo, shopuid
  loadItemList: function (pageNo, shopuid, cityName){

    console.log("loadItemList分页加载全部商品列表--参数 pageNo, shopuid");
    let that = this;
    //获取商品列表-------------------------------------------------------
    http.getReq('/weixin/item/itemList', {
      pageNo: pageNo || 1,
      pageSize: app.data.pageSize,
      shopuid: shopuid,//用于根据店铺id查找该店铺上架的商品
      cityName: cityName,//城市名称
    }, function (res) {
     
      let products = [];
      if (pageNo > 1) {
        products = that.data.products;
      }

      // 数据加载成功 停止下拉刷新
      let newList = res.data

      console.log("分页总数total==" + res.total);
      
      //获取商品最大最小价格
      if (newList.length > 0){
        app.getNewList(newList);
        newList = app.data.itemList;
        products.push(...newList);//在原数组基础上进行拼接
      }
      
      that.setData({
        products: products,
        pageNo: pageNo || 1,
        total: res.total,
        showLoadding: false    //隐藏加载动画
      })
      //改变图片高度
      wx.stopPullDownRefresh();

      console.log("请求返回商品列表长度" + that.data.products.length);
      console.log(that.data.products);
      
    });
  },
  //你想解析用户坐标获取城市
  loadCity: function (longitude, latitude){

    console.log('解析地理位置信息');
    
    let that = this;
    // 调用接口
    demo.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        
        console.log('解析地理位置信息');
        let cityName = res.result.address_component.city;

        //开放两个城市
        cityName = (cityName == '天津市') ? '天津' : '北京';
        //同步数据，保存到本地
        that.setCityName(cityName);
       
      },
      fail: function (res) {
        console.log('解析地理位置失败');
        that.setCityName();
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  //设置城市
  setCityName:function(cityName){

    console.log('setCityName==' + cityName);

    app.setCityName(cityName);
    
    this.setData({
      cityName: app.data.cityName 
    })

    this.getCarouseImgArr();//根据城市切换首页banner
  },

  //改变选择城市
  onCityChange:function(e){
    
    let that = this;
    app.onCityChange(e);

    let cityName = app.data.cityName;
    that.setData({
      cityName: cityName,
      areaIcon: !this.data.areaIcon,
      products: [],
      hotPorducts:[]
    })

    that.setCityName(cityName);//更改本地city名称

    //根据城市查询商品
    let shopInfo = wx.getStorageSync("shopInfo");
    let [pageNo, shopuid] = [1, ''];
    shopuid = shopInfo.shopuid || shopuid;
    
    that.loadHotItemList(shopuid, cityName);//加载热门商品列表
    that.loadItemList(pageNo, shopuid, cityName); //加载普通商品列表（分页）
  },
  onCityCancel:function(){
    this.setData({
      areaIcon: !this.data.areaIcon
    })
  },
  //更换地域选择图标
  changeAreaIcon:function(){
    this.setData({
      areaIcon: !this.data.areaIcon
    })
  },
  //通知滚动事件
  // noticeScroll:function(e){
  //   console.log('通知滚动回调');
  //   console.log(e);

  // },
  //上新滚动条滚动：
  // noticeRoll:function(){
  //   let that = this;
  //   let isBottom = that.data.isBottom;//是否触底
  //   let noticeScrollTop = that.data.noticeScrollTop;//距离顶部距离
  //   var loop = setInterval(function(){
  //     if (!isBottom){
  //       noticeScrollTop = noticeScrollTop + 5
  //       that.setData({
  //         noticeScrollTop: noticeScrollTop++
  //       })
  //     }
  //   },500)
   
  // },
  // noticLower:function(e){
  //   console.log('已经触底了');
  //   console.log(e);

  // }
}); 